<?php
/**
 * 更新品牌荣耀（品牌榜）榜单的连续上榜次数
 * 更新时间：每周三，在榜单张榜之后。每周三0点3分
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\WikiRankingBrand;
use Biz\model\wiki\WikiRankingBrandData;
use Biz\model\wiki\WikiRankingHot;
use Biz\model\wiki\WikiRankingPublicPraise;
use Biz\model\wiki\WikiRankingWeek;
use Biz\service\wikiRanking\Ranking;

$start = time();
$dayArr = Ranking::getCreateRankingDay();
$day = $dayArr['day'];
$now = date('Ymd');
if ($day != $now) {
    var_dump('当前日期为：' . $now . '不能更新第「' . $day . '」的品牌荣耀榜单');
    return false;
}
$info = WikiRankingWeek::getByWhere(['ranking_day' => $day]);
if (!$info) {
    var_dump('不存在的榜单期数');
    return false;
}

//重置品牌 榜单的连续上榜次数
$res = WikiRankingBrandData::updateData(['continue_week' => 0], ['id[>]' => 0 ,'data_type' => [WikiRankingBrandData::TYPE_HOT ,WikiRankingBrandData::TYPE_USER_LIKE , WikiRankingBrandData::TYPE_BRAND ]]);

$allWeekDay = WikiRankingWeek::selectByWhere(['ORDER' => ['ranking_day' => 'DESC'], 'ranking_day[<=]' => $day], 'ranking_day');

$currentRankingBrandData = [
    WikiRankingBrandData::TYPE_HOT => [],
    WikiRankingBrandData::TYPE_USER_LIKE => [],
    WikiRankingBrandData::TYPE_BRAND => [],
];
$allRankingBrandData = [
    WikiRankingBrandData::TYPE_HOT => [],
    WikiRankingBrandData::TYPE_USER_LIKE => [],
    WikiRankingBrandData::TYPE_BRAND => [],
];

$allBrandIds = WikiRankingBrand::selectByWhere(['ranking_state' => WikiRankingBrand::RANKING_STATE_SHOW], 'brand_id');
$allBrandIds = array_unique($allBrandIds);

$hotWhere = ['ranking_state' => WikiRankingHot::RANKING_STATE_SHOW, 'brand_id[>]' => 0];
$publicPraiseWhere = ['ranking_state' => WikiRankingPublicPraise::RANKING_STATE_SHOW, 'brand_id[>]' => 0];
$brandWhere = ['ranking_state' => WikiRankingBrand::RANKING_STATE_SHOW];
foreach ($allWeekDay as $index => $day) {
    $hotWhere['ranking_day'] = $day;
    $hotWhere['brand_id'] = $allBrandIds;

    $publicPraiseWhere['ranking_day'] = $day;
    $publicPraiseWhere['brand_id'] = $allBrandIds;

    $brandWhere['ranking_day'] = $day;

    if ($index > 0) {
        if (!isset($hotWhere['ranking_id']) || !$hotWhere['ranking_id']) {
            $hotWhere = [];
        }
        if (!isset($publicPraiseWhere['ranking_id']) || !$publicPraiseWhere['ranking_id']) {
            $publicPraiseWhere = [];
        }
        if (!isset($brandWhere['ranking_id']) || !$brandWhere['ranking_id']) {
            $brandWhere = [];
        }
    }
    if ($hotWhere) {
        $hotRankingArr = WikiRankingHot::selectByWhere($hotWhere);
        $brandIds = array_column($hotRankingArr, 'brand_id');
        $allBrandIds = array_merge($allBrandIds, $brandIds);
        $hotWhere['ranking_id'] = [];
        foreach ($hotRankingArr as $row) {
            $key = $row['brand_id'] . '_' . $row['ranking_id'];
            if ($index == 0) {
                $currentRankingBrandData[WikiRankingBrandData::TYPE_HOT][$key] = 1;
                $allRankingBrandData[WikiRankingBrandData::TYPE_HOT][$key] = 1;
                $hotWhere['ranking_id'][] = $row['ranking_id'];
            } else {
                if (isset($currentRankingBrandData[WikiRankingBrandData::TYPE_HOT][$key])) {
                    $hotWhere['ranking_id'][] = $row['ranking_id'];
                }
                if ($allRankingBrandData[WikiRankingBrandData::TYPE_HOT][$key] == $index) {
                    $allRankingBrandData[WikiRankingBrandData::TYPE_HOT][$key] = $index + 1;
                }
            }
        }
    }
    if ($publicPraiseWhere) {
        $publicPraiseRankingArr = WikiRankingPublicPraise::selectByWhere($publicPraiseWhere);
        $brandIds = array_column($publicPraiseRankingArr, 'brand_id');
        $allBrandIds = array_merge($allBrandIds, $brandIds);
        $allBrandIds = array_unique($allBrandIds);
        $publicPraiseWhere['ranking_id'] = [];
        foreach ($publicPraiseRankingArr as $row) {
            $key = $row['brand_id'] . '_' . $row['ranking_id'];
            if ($index == 0) {
                $currentRankingBrandData[WikiRankingBrandData::TYPE_USER_LIKE][$key] = 1;
                $allRankingBrandData[WikiRankingBrandData::TYPE_USER_LIKE][$key] = 1;
                $publicPraiseWhere['ranking_id'][] = $row['ranking_id'];
            } else {
                if (isset($currentRankingBrandData[WikiRankingBrandData::TYPE_USER_LIKE][$key])) {
                    $publicPraiseWhere['ranking_id'][] = $row['ranking_id'];
                }
                if ($allRankingBrandData[WikiRankingBrandData::TYPE_USER_LIKE][$key] == $index) {
                    $allRankingBrandData[WikiRankingBrandData::TYPE_USER_LIKE][$key] = $index + 1;
                }
            }
        }
    }
    if ($brandWhere) {
        $brandRankingArr = WikiRankingBrand::selectByWhere($brandWhere);
        $brandWhere['ranking_id'] = [];
        foreach ($brandRankingArr as $row) {
            $key = $row['brand_id'] . '_' . $row['ranking_id'];
            if ($index == 0) {
                $currentRankingBrandData[WikiRankingBrandData::TYPE_BRAND][$key] = 1;
                $allRankingBrandData[WikiRankingBrandData::TYPE_BRAND][$key] = 1;
                $brandWhere['ranking_id'][] = $row['ranking_id'];
            } else {
                if (isset($currentRankingBrandData[WikiRankingBrandData::TYPE_BRAND][$key])) {
                    $brandWhere['ranking_id'][] = $row['ranking_id'];
                }
                if ($allRankingBrandData[WikiRankingBrandData::TYPE_BRAND][$key] == $index) {
                    $allRankingBrandData[WikiRankingBrandData::TYPE_BRAND][$key] = $index + 1;
                }
            }
        }
    }
}

foreach ($allRankingBrandData as $type => $rows) {
    if ($rows) {
        foreach ($rows as $key => $continueWeek) {
            $arr = explode('_', $key);
            $brandId = $arr[0];
            $rankingId = $arr[1];
            addData($brandId, $rankingId, $continueWeek, $type);
        }
    }
}

$end = time();
var_dump(date('Y-m-d-H-i-s') . '第' . $day . '期榜单,更新品牌荣耀（品牌榜）榜单的连续上榜次数,耗时:' . ($end - $start) . '秒，' . (($end - $start) / 60) . '分');

function addData($brandId, $rankingId, $continueWeek, $type)
{
    $data = [
        'brand_id' => $brandId,
        'ranking_id' => $rankingId,
        'data_type' => $type,
        'continue_week' => $continueWeek,
    ];
    $info = WikiRankingBrandData::getByWhere(['brand_id' => $brandId, 'ranking_id' => $rankingId, 'data_type' => $type]);;
    if ($info) {
        WikiRankingBrandData::updateData($data, ['id' => $info['id']]);
    } else {
        WikiRankingBrandData::insertData($data);
    }
}